package com.dhnsoft.education.controller.edu;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhnsoft.education.entity.edu.EduCourse;
import com.dhnsoft.education.entity.edu.vo.CourseInfoVo;
import com.dhnsoft.education.entity.edu.vo.CoursePublishVo;
import com.dhnsoft.education.entity.edu.vo.CourseQuery;
import com.dhnsoft.education.service.edu.EduCourseService;
import com.dhnsoft.education.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 代浩楠
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //添加课程信息
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }
    //获取课程信息后续进行修改（发布课程模块）
    @GetMapping("getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId){
        CourseInfoVo courseInfoForm = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoForm",courseInfoForm);

    }
    //修改课程(发布课程模块)
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        System.out.println(coursePublishVo);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @GetMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus("Normal");
        eduCourse.setId(id);
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表 基本实现
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        System.out.println("111111"+list);
        return R.ok().data("list",list);
    }
    //条件查询带分页的方法
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current, @PathVariable long limit, @RequestBody CourseQuery courseQuery){
        Page<EduCourse> pageCourse = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.like("status",status);
        }
        //调用方法实现条件查询分页
        courseService.page(pageCourse,wrapper);
        long total = pageCourse.getTotal();//总记录数
        List<EduCourse> records = pageCourse.getRecords();//数据list集合数
        return R.ok().data("total",total).data("rows",records);
    }

    //课程列表中删除课程方法
    @DeleteMapping("removeCourseById/{courseId}")
    public R removeCourseById(@PathVariable String courseId){
        courseService.removeCourse(courseId);

        return R.ok();
    }

    //远程调用根据课程id获取课程信息
    @GetMapping("getCourseInfoByClient/{courseId}")
    public EduCourse getCourseInfoByClient(@PathVariable String courseId){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",courseId);
        EduCourse course = courseService.getOne(wrapper);
        return course;
    }

    //根据课程Id获取版权信息
    @GetMapping("getCopyrightById/{id}")
    public R getCopyrightById(@PathVariable String id){
        EduCourse course = courseService.getById(id);
        return R.ok().data("course",course);
    }


}

